package v1

import (
	"bc/service"
	"bc/util"
	"encoding/json"
	"github.com/gin-gonic/gin"
	"io/ioutil"
	"strconv"
)

type os struct {

}
func GetNode(byte []byte) []interface{}{

	data := make(map[string]interface{})
	err := json.Unmarshal(byte,&data)
	if err != nil {
		panic(err)
	}

	edges := data["edges"].([]interface{})
	nodes := data["nodes"].([]interface{})

	for k,v := range nodes{

		source := v.(map[string]interface{})["id"]

		for _,vv := range edges{
			if source == vv.(map[string]interface{})["source"]{
				nodes[k].(map[string]interface{})["children"] = append(nodes[k].(map[string]interface{})["children"].([]interface{}),vv)
			}
		}

	}
	return nodes
}
func Trees(byte []interface{})[]interface{}{

	//for _,v := range byte{
	//	child := v.(map[string]interface{})["children"]
	//	for _,vv := range child.([]interface{}){
	//
	//	}
	//}
	return byte

}
func (t *os)CreateN(c *gin.Context){

	byte,err := ioutil.ReadAll(c.Request.Body)
	defer c.Request.Body.Close()
	if err != nil {
		c.JSON(200,gin.H{
			"code":500,
			"msg":"error",

		})
		return
	}

	result := Trees(GetNode(byte))
	c.JSON(200,gin.H{
		"data":result,
	})

	//for k,v := range edges{
	//
	//	v.(map[string]interface{})["children"] = make([]map[string]interface{},10)
	//	child := v.(map[string]interface{})["children"].([]map[string]interface{})
	//
	//	for _,vv := range nodes{
	//
	//		if v.(map[string]interface{})["source"] == vv.(map[string]interface{})["id"]{
	//
	//			child = append(child,vv.(map[string]interface{}))
	//			edges[k].(map[string]interface{})["children"] = child
	//
	//		}
	//
	//	}
	//
	//}
	//b,_ :=json.Marshal(edges)
	//fmt.Println(string(b))
	/*
	var osd dao.OS
	err := c.ShouldBind(&osd)

	if err != nil{
		c.JSON(200,gin.H{
			"code":500,
			"msg":"error",
			"data":err.Error(),
		})
		return
	}
	fmt.Println(osd)

	bool := dao.OSdao.CreateN(&osd)
	fmt.Println(bool)*/

}
func (t *os)Create(c *gin.Context){

	d := map[string]interface{}{}
	if v,ok := c.GetPostForm("pid");ok{
		pid, err := strconv.Atoi(v)
		if err != nil{
			c.JSON(200,gin.H{
				"code":500,
				"msg":"error",
				"data":"数据有误，请重试",
			})
			return
		}
		d["pid"] = pid
	}else{
		d["pid"] = 0
	}
	if v,ok := c.GetPostForm("name");ok{
		d["name"] = v
	}
	if v,ok := c.GetPostForm("desc");ok{
		d["desc"] = v
	}
	if v,ok := c.GetPostForm("status");ok{
		status,err := strconv.Atoi(v)
		if err != nil{
			c.JSON(200,gin.H{
				"code":500,
				"msg":"error",
				"data":"数据有误，请重试",
			})
			return
		}
		d["status"] = status
	}else{
		d["status"] = 1
	}
	b,_ := service.OS.FirstByName(d["name"].(string))
	if b {
		c.JSON(200,gin.H{
			"code":500,
			"msg":"error",
			"data":"该镜像名称已存在",
		})
		return
	}
	b,last_id := service.OS.Create(d)
	if b {
		LogAdd(c,"添加操作系统" + strconv.Itoa(last_id),"操作系统管理")
		c.JSON(200,gin.H{
			"code":200,
			"msg":"操作成功",
			"data":"成功",
		})
		return
	}
	c.JSON(200,gin.H{
		"code":500,
		"msg":"error",
		"data":"失败",
	})
}
func (t *os)Delete(c *gin.Context){

	os_id_str := c.Param("os_id")
	os_id,err := strconv.Atoi(os_id_str)
	if err != nil{
		c.JSON(200,gin.H{
			"code":500,
			"msg":"error",
			"data":"数据有误",
		})
		return
	}

	b := service.OS.Delete(os_id)
	if b {
		LogAdd(c,"删除操作系统" + os_id_str,"操作系统管理")
		c.JSON(200,gin.H{
			"code":200,
			"msg":"操作成功",
			"data":"成功",
		})
		return
	}
	c.JSON(200,gin.H{
		"code":500,
		"msg":"error",
		"data":"错误，请重试",
	})

}
func (t *os)Update(c *gin.Context){

	os_id_str := c.Param("os_id")
	os_id,err := strconv.Atoi(os_id_str)

	if err != nil {
		c.JSON(200,gin.H{
			"code":500,
			"msg":"error",
			"data":"错误，请重试",
		})
	}
	f := map[string]interface{}{}

	if v,ok := c.GetPostForm("pid");ok{

		pid,_ := strconv.Atoi(v)
		f["pid"] = pid

	}
	if v,ok := c.GetPostForm("name");ok{
		f["name"] = v
	}
	if v,ok := c.GetPostForm("desc");ok{
		f["desc"] = v
	}
	if v,ok := c.GetPostForm("status");ok{
		status,_ := strconv.Atoi(v)
		f["status"] = status
	}
	b := service.OS.Update(os_id,f)
	if b {
		LogAdd(c,"修改操作系统" + os_id_str,"操作系统管理")
		c.JSON(200,gin.H{
			"code":200,
			"msg":"操作成功",
			"data":"成功",
		})
		return
	}
	c.JSON(200,gin.H{
		"code":500,
		"msg":"error",
		"data":"失败",
	})
}
func (t *os)Find (c *gin.Context){

	w := make(map[string]interface{})

	if v,ok := c.GetPostForm("pid");ok{
		pid,_ := strconv.Atoi(v)
		w["pid"] = pid

	}
	if v,ok := c.GetPostForm("name");ok{
		w["name"] = v
	}

	if v,ok := c.GetPostForm("status");ok {
		status, _ := strconv.Atoi(v)
		w["status"] = status
	}

	page := int64(1)

	if v,ok := c.GetPostForm("page");ok{
		p,_ := strconv.ParseInt(v,10,64)
		page = p
	}

	pageSize := util.Config.App.PageSize

	result,total,err := service.OS.Find(w,page,pageSize)

	if err != nil{
		c.JSON(200,gin.H{
			"code":500,
			"msg":"error",
			"data":"",
			"total":0,
		})
		return
	}
	c.JSON(200,gin.H{
		"code":200,
		"msg":"操作成功",
		"data":result,
		"total":total,
	})

}
func (t *os)First(c *gin.Context){
	os_id_str := c.Param("os_id")
	os_id,_ := strconv.Atoi(os_id_str)
	b,data := service.OS.First(os_id)
	if b {
		c.JSON(200,gin.H{
			"code":200,
			"msg":"操作成功",
			"data":data,
		})
		return
	}
	c.JSON(200,gin.H{
		"code":500,
		"msg":"error",
		"data":"错误",
	})
}

var OS = &os{}
